//HDU 1370
#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;

int exgcd(const int &a,const int &b,int &x,int &y){
    if(b){
        int ans=exgcd(b,a%b,x,y);
        int t=x;x=y;y=t-a/b*y;
        return ans;
    }else{
        x=1;y=0;
        return a;
    }
}

int ine(int a,int p){
    int x,y;
    int g=exgcd(a,p,x,y);
    //assert(g==1);
    return (x%p+p)%p;
}

int crt(const int *a,const int *p,const int &n){
    int M=1,ans;
    for(int i=0;i<n;i++) M*=p[i];
    ans=0;
    for(int i=0;i<n;i++) ans+=a[i]*(M/p[i])%M*ine(M/p[i],p[i])%M,ans%=M;
    return ans;
}

int a[3];
const int p[3]={23,28,33};
const int M=23*28*33;
const char *format="Case %d: the next triple peak occurs in %d days.\n";

int main(){
    int kase=0,T;

    scanf("%d",&T);
    while(T--){
        while(~scanf("%d%d%d",a,a+1,a+2)){
            if(a[0]==-1) break;
            int t;
            scanf("%d",&t);
            t%=M;
            for(int i=0;i<3;i++) a[i]%=p[i];
            int ans=crt(a,p,3);
            ans-=t;
            if(ans<=0) ans+=M;
            printf(format,++kase,ans);
        }
        if(T) putchar('\n');
    }

    return 0;
}
